﻿/*
 * Atmega8_Testowa_test.c
 *
 * Created: 2014-03-09 22:19:45
 *  Author: Michał
 */ 


#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include "main.h"
#include "one_wire.h"

char buf[8];
void Init(void);


volatile unsigned char odb_x;       //odebrana liczba X
volatile unsigned char odb_flaga =0;

volatile uint8_t ds18b20_status_read;


ISR(USART_RXC_vect)
{

	odb_x = UDR;   //zapamiętaj odebraną liczbę
	odb_flaga = 1; //ustaw flagę odbioru liczby dla main()
}



/**
  * Brief: This function received key from keyboard.
  * Parameters:   None
  * Return value: Received key
  */
uint8_t DIP_Switch_Read(void)
{
	uint8_t tmp=0, i=8;
	
	LD_PULSE;
	while(i)
	{
		if(DIP_SWITCH_STATE) tmp |= (0x01 << (i-1));
		CLK_PULSE;
		i--;
	}
	LD_LOW;
	return tmp;
}/*
 ***** End function ************************************************************
 -------------------------------------------------------------------------------
 */


/**
  * Brief: This function received key from keyboard.
  * Parameters:   None
  * Return value: Received key
  */
void LEDS_Send(uint8_t data)
{
	int i=8;
	
	while(i--)
	{
		if(data&0x80) SI_HIGH;
		else SI_LOW;
		CLK_PULSE;
		data <<= 1;
	}
	STCP_LEDS_PULSE;
	SI_LOW;
}/*
 ***** End function ************************************************************
 -------------------------------------------------------------------------------
 */


void DS18B20_Temp_Read(void)
{
	uint8_t temp1 = 0, temp2 = 0;
	float temp;

	OneWire_Reset();
	OneWire_SendByte(0xCC);
	OneWire_SendByte(0xBE);
	temp1=OneWire_ReadByte();
	temp2=OneWire_ReadByte();
	OneWire_Reset();
	temp=((float)(temp1+temp2*256))/16;
	dtostrf(temp,1,1,buf);
	OneWire_Reset();
	OneWire_SendByte(0xCC);
	OneWire_SendByte(0x44);
	ds18b20_status_read = 0;
}

void DS18B20_Temp_Conv(void)
{
	if(!OneWire_Reset())
	{
		
	}
}


void Timer1_Init(void)
{
	TCCR1B = (1<<CS12) | (1<<CS10); //Prescaler clk0/1024
	TCNT1 = 59636;
	TIMSK = (1<< TOIE1);			//Timer/Counter1, Overflow Interrupt Enable
}

ISR(TIMER1_OVF_vect)
{
	ds18b20_status_read = 1;
	TCNT1=59636;
}

/**
  * Brief: This function received key from keyboard.
  * Parameters:   None
  * Return value: Received key
  */
int main(void)
{
	uint8_t a=0;

	Init();
	LCD_Home();
	LCD_Text("Hello");
	_delay_ms(1000);
	LCD_Clear();
	LCD_Position_LN(1, 1);
	LCD_Text("Temp: ");
	sei();

	
    while(1)
    {
		if(ds18b20_status_read){
			if(!OneWire_Reset()){
				DS18B20_Temp_Read();
				LCD_Position_LN(1, 7);
				LCD_Text(buf);
			}
			else{
				LCD_Position_LN(1, 7);
				LCD_Text("ERROR");
			}
		}

		a = DIP_Switch_Read();
		LEDS_Send(a);
    }
}/*
 ***** End function ************************************************************
 -------------------------------------------------------------------------------
 */


/**
  * Brief: This function received key from keyboard.
  * Parameters:   None
  * Return value: Received key
  */
void Init(void)
{
	LCD_Init();
	_delay_ms(100);
	CLK_DDR |= (1<<CLK_PIN);
	CLK_LOW;
	
	SI_DDR |= (1<<SI_PIN);
	SI_LOW;
	
	LD_DDR |= (1<<LD_PIN);
	LD_LOW;
	
	STCP_LEDS_DDR |= (1<<STCP_LEDS_PIN);
	STCP_LEDS_LOW;
	
	LED_GREEN_DDR |= (1<<LED_GREEN_PIN);
	LED_GREEN_OFF;
	
	DIP_SWITCH_DDR &= ~(1<<DIP_SWITCH_PIN_NUM);
	DIP_SWITCH_PORT |= (1<<DIP_SWITCH_PIN_NUM);
	
	SWITCH1_DDR &= ~(1<<SWITCH1_PIN_NUM);
	SWITCH1_PORT |= (1<<SWITCH1_PIN_NUM);
	
	SWITCH2_DDR &= ~(1<<SWITCH2_PIN_NUM);
	SWITCH2_PORT |= (1<<SWITCH2_PIN_NUM);

	_delay_ms(20);
	LEDS_Send(0);	
	
	USART_Init();
	Timer1_Init();
	
	
	ONE_WIRE_PIN_IN;
	
}/*
 ***** End function ************************************************************
 -------------------------------------------------------------------------------
 */
/* END OF FILE *****************************************************************
 *
 */
